<?php
$temp = $this->getFormdata();
$imgurl = $this->getSkinUrl('/images/ajax-loader-tr.gif');
?>
<div class="entry-edit">
    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend">商品查询类别</h4>
        <div class="form-buttons"></div>
<!--        <div class="close" style="float: right;color: #fff;padding: 0 5px;cursor: pointer;">关闭</div>-->
    </div>
    <div class="fieldset  ignore-validate" id="coupons_information_fieldset">
        <div class="hor-scroll" style="height:200px;">
            <table cellspacing="0" class="form-list">
                <tbody>
                <tr>
                    <td class="label"><label for="sku">商品SKU</label></td>
                    <td class="value">
                        <input id="sku" value="" title="Dash Every X Characters" class="input-text"/><p class="note" id="note_dash"><span>参与活动商品的SKU,优先以SKU查找</span></p>
                    </td>
                </tr>

                <tr>
                    <td class="label"><label for="sku">商品ID</label></td>
                    <td class="value">
                        <input id="productid" value="" class="input-text"/><p class="note" id="note_dash"><span>参与活动商品的ID</span></p>
                    </td>
                </tr>

                <tr>
                    <td class="label"><label for="sku">产品名称</label></td>
                    <td class="value">
                        <input id="productname" value="" class="input-text"/><p class="note" id="note_dash"><span>参与活动商品名称</span></p>
                    </td>
                </tr>


                <tr>
                    <td class="label"></td>
                    <td class="value">
                        <span><button  title="查找名称" type="button" class="scalable generate" onclick="checks()"><span>查找</span></button></span>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="entry-edit-head">
        <h4 class="icon-head head-edit-form fieldset-legend">商品列表</h4>
        <div class="form-buttons"></div>
    </div>
    <div class="fieldset  ignore-validate" id="coupons_information_fieldset">
        <div class="grid np">
            <div class="hor-scroll">
                <table cellspacing="0" class="data order-tables">
                    <colgroup>
                        <col width="1">
                        <col width="1">
                        <col width="1">
                        <col width="1">
                        <col width="1">
                        <col>
                    </colgroup>
                    <thead>
                    <tr class="headings">
                        <th><span>商品ID</span></th>
                        <th><span class="nobr">商品sku</span></th>
                        <th><span class="nobr">商品名称</span></th>
                        <th><span class="nobr">商品图片</span></th>
                        <th><span class="nobr">操作</span></th>
                    </tr>
                    </thead>
                    <tbody class="even" id="appendproduct">
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey()?>">

<script type="text/javascript">
    var  $j = jQuery.noConflict();
    function addsku(element){
        $j('#addsku').show();
        if ($j(element).attr('data-id')){
            $j('#addsku').attr('data-id',$j(element).attr('data-id'));
        }
        window.addsku_list=$j(element).parent().parent().next().find('td ul');
        layer.open({
            type: 1,
            title: false,
            closeBtn:1,
            title:'商品查询',
            maxmin: true,
            area: ['600px', '600px'],
            skin: 'layui-layer-rim', 
            moveType: 1,
            shift: 2,
            content: $j('#addsku'),
            cancel:function(index){
                $j('#sku').val('');
                $j('#productid').val('');
                $j('#productname').val('');
                $j('#appendproduct').html('');}
        });
    }

    //rewrite check function to search product By Id,name,Sku
    function checks(){
        var sku = $j("#sku").val();
        var productid = $j("#productid").val();
        var productname = $j("#productname").val();
        var websiteid = <?php echo $temp['website_id']?>;
        if(!sku && !productname && !productid){
            alert("请输入商品SKU,商品ID,商品名称任意一项进行搜索!");
            return false;
        }

        var productsku = new Array();
        $j("#appendproduct .productsku").each(function(){
            productsku.push($j(this).html());
        });

        var productnames = new Array();
        $j("#appendproduct .productnames").each(function(){
            productnames.push($j(this).html());
        });

        var productids = new Array();
        $j("#appendproduct .productids").each(function(){
            productids.push($j(this).html());
        });


        if($j.inArray(sku, productsku)>=0 || $j.inArray(productid, productids)>=0){
            alert("该商品已在该列表中!");
            return false;
        }

        if(productname){
            for(var i=0;i<productnames.length;i++){
                if(productnames[i].indexOf(productname)!=-1)
                {
                    alert("商品列表中已有相关产品!");
                    return false;
                }
            }
        }

        var url ="<?php echo $this->getUrl('admin_promotion/adminhtml_activity/productlist'); ?>";

        if(sku){
            var url = url + "sku/"+sku+"/websiteid/"+websiteid;
        }else if(productid){
            var url = url + "productid/"+productid+"/websiteid/"+websiteid;
        }else if(productname){
            var url = url +"productname/"+productname+"/websiteid/"+websiteid;
        }

        $j.get(url,function(result){
            if(result.error){
                alert(result.message);

            }else{
                var html = result.message;
                document.getElementById("sku").value="";
                document.getElementById("productname").value="";
                document.getElementById("productid").value="";
                $j("#appendproduct").append(html);
                //删除活动商品
                $j(".remove").bind("click",function(){
                    $j(this).parent().parent('.parent').remove();
                });
            }

        },"json");
    }


    $j(document).ready(function(){
        $j('#addsku').on('click','.add',function(){
            var t_element=$j(this).parent().parent().find('td');
            if(window.addsku_list&&window.addsku_list.find('input[value="'+t_element.eq(1).html()+'"]').length<=0){
                window.addsku_list.append('<li style="overflow: hidden;display: block;white-space: nowrap;-ms-text-overflow: ellipsis;text-overflow: ellipsis;"><input type="hidden" name="form_data[sku'+$j('#addsku').attr('data-id')+'][]" value="'+t_element.eq(1).html()+'">'+t_element.eq(2).html()+'<span class="del_product" style="float: right;cursor: pointer;color: red;">删除</span></li>');
                alert('商品添加成功！');
            }else{
                alert('该商品已在列表中！');
            }
            t_element.parent().remove();
        });
        $j('body').on('click','.del_product',function(){
            var flag = confirm("确认要删除该商品？");
            if(flag){
                $j(this).parent().remove();
            }
        });

    });

</script>

